package com.chuhe.common.utils;

import com.chuhe.common.constans.MyConstants;
import com.chuhe.common.domain.dto.SysSettingDto;
import com.chuhe.common.domain.dto.TokenUserInfoDto;
import com.chuhe.common.domain.vo.resp.SysSettingResp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @Author: 楚河
 * @Date: 2025年05月11日16:59
 */
@Component
public class RedisComponet {
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 保存用户登录信息
     * @Author: 楚河
     * @Date: 2025-05-13 21:52
     */
    public void saveTokenUserInfoDto(TokenUserInfoDto tokenUserInfoDto) {
        redisUtils.setex(MyConstants.REDIS_KEY_WS_TOKEN + tokenUserInfoDto.getToken(),tokenUserInfoDto,MyConstants.REDIS_KEY_TOKEN_EXPIRES);
        redisUtils.setex(MyConstants.REDIS_KEY_WS_TOKEN_USERID + tokenUserInfoDto.getUserId(),tokenUserInfoDto.getToken(),MyConstants.REDIS_KEY_TOKEN_EXPIRES);
    }
    /**
     * 获取用户登录信息
     * @Author: 楚河
     * @Date: 2025-05-13 21:53
     */
    public TokenUserInfoDto getTokenUserInfoByToken(HttpServletRequest request){
        String token = request.getHeader("token");
        TokenUserInfoDto tokenUserInfoDto = (TokenUserInfoDto) redisUtils.get(MyConstants.REDIS_KEY_WS_TOKEN + token);
        return tokenUserInfoDto;
    }

    public SysSettingResp getSysSetting() {
        SysSettingDto sysSettingDto = (SysSettingDto)redisUtils.get(MyConstants.REDIS_KEY_SYS_SETTING);
        sysSettingDto = Objects.isNull(sysSettingDto) ? new SysSettingDto() : sysSettingDto;
        SysSettingResp sysSettingResp = new SysSettingResp();
        BeanUtils.copyProperties(sysSettingDto,sysSettingResp);
        return sysSettingResp;
    }
    /**
     * 添加用户联系人缓存
     * @Author: 楚河
     * @Date: 2025-05-17 11:21
     */
    public void addUserContact(String userId, String contactId) {
        List<String> contactList = redisUtils.getQueueList(MyConstants.REDIS_KEY_USER_CONTACT + userId);
        if(!contactList.contains(contactId)){
            redisUtils.lpush(MyConstants.REDIS_KEY_USER_CONTACT + userId,contactId,MyConstants.REDIS_KEY_TOKEN_EXPIRES);
        }
    }
    /**
     * 删除好友缓存
     * @Author: 楚河
     * @Date: 2025-05-17 17:13
     */
    public void removeuserContact(String userId, String contactId) {
        redisUtils.remove(MyConstants.REDIS_KEY_USER_CONTACT + userId,contactId);
    }
    /**
     * 获取token中的昵称
     * @Author: 楚河
     * @Date: 2025-05-17 23:11
     */
    public TokenUserInfoDto getTokenuserInfoDtoByUserId(String userId) {
        String token = (String) redisUtils.get(MyConstants.REDIS_KEY_WS_TOKEN_USERID + userId);
        return getTokenUserInfoDto(token);
    }
    /**
     * 获取token中的信息
     * @Author: 楚河
     * @Date: 2025-05-17 23:12
     */
    public TokenUserInfoDto getTokenUserInfoDto(String token) {
        TokenUserInfoDto tokenUserInfoDto = (TokenUserInfoDto) redisUtils.get(MyConstants.REDIS_KEY_WS_TOKEN + token);
        return tokenUserInfoDto;
    }

    public void saveSysSetting(SysSettingDto sysSettingDto) {
        redisUtils.set(MyConstants.REDIS_KEY_SYS_SETTING, sysSettingDto);
    }
    /**
     * 保存心跳
     * @Author: 楚河
     * @Date: 2025-05-19 22:06
     */
    public void saveUserHeartBeat(String userId) {
        redisUtils.setex(MyConstants.REDIS_KEY_WS_USER_HEART_BEAT + userId, System.currentTimeMillis(), MyConstants.REDIS_KEY_EXPIRES_HEART_BEAT);
    }
    /**
     * 获取用户联系人Id
     * @Author: 楚河
     * @Date: 2025-05-21 16:50
     */
    public List<String> getUserContactIdList(String userId) {
        return redisUtils.getQueueList(MyConstants.REDIS_KEY_USER_CONTACT + userId);
    }
    /**
     * 删除心跳缓存
     * @Author: 楚河
     * @Date: 2025-05-21 17:01
     */
    public void removeUserHeartBeat(String userId) {
        redisUtils.delete(MyConstants.REDIS_KEY_WS_USER_HEART_BEAT + userId);
    }

    /**
     * 清除用户token
     * @Author: 楚河
     * @Date: 2025-05-22 16:16
     */
    public void cleanUserTokenByUserId(String userId) {
        String token = (String) redisUtils.get(MyConstants.REDIS_KEY_WS_TOKEN_USERID + userId);
        if (!StringTools.isEmpty(token)) {
            redisUtils.delete(MyConstants.REDIS_KEY_WS_TOKEN + token);
        }
    }
    /**
     * 移除群组联系人
     * @Author: 楚河
     * @Date: 2025-05-22 16:22
     */
    public void removeUserContact(String userId, String contactId) {
        redisUtils.remove(MyConstants.REDIS_KEY_USER_CONTACT + userId, contactId);
    }

    public List<String> getUserContactList(String userId) {
        return redisUtils.getQueueList(MyConstants.REDIS_KEY_USER_CONTACT + userId);
    }
}
